package com.aosgame;

import android.graphics.Bitmap;
import android.graphics.Rect;

import com.androidgame.fx.Graphics;
import com.androidgame.fx.ScreenConfig;
import com.androidgame.fx.Input.TouchEvent;

public class AttackPad {

	private Bitmap 	Image;
	private Bitmap 	onImage;
	private Rect	padArea;
	private Player	player;
	private Minion[] minions;
	private boolean	touched;
	private int 	touchPointer;
	
	public AttackPad(Player player, Minion[] minions) {
		this.player = player;
		this.minions = minions;
		
		Image = Assets.attackPad;
		onImage = Assets.onAttackPad;
		int scrWidth = ScreenConfig.virtualScreenWidth;
		int scrHeight = ScreenConfig.virtualScreenHeight;
		int right = (int)(scrWidth * 0.95);
		int bottom = (int)(scrHeight * 0.95);
		padArea = new Rect(right - Image.getWidth(), bottom - Image.getHeight(), right, bottom);
		
		touchPointer = -1;
	}

	public boolean touchDown(TouchEvent event) {
		if(padArea.contains(event.x, event.y)) {
			touchPointer = event.pointer;
			touched = true;
			
			if((player.state & Player.STATE_ATTACK) != Player.STATE_ATTACK) {
				int distance = 10000;
				int targetIndex = 0;
				int tempDist;
				
				for(int i = 0; i < minions.length; i++) {
					tempDist = Math.abs(player.position.x - minions[i].position.x) + 
							Math.abs(player.position.y - minions[i].position.y);
					if(distance > tempDist) {
						distance = tempDist;
						targetIndex = i;
					}
				}
					
				player.target = minions[targetIndex];
				
				player.addState(Player.STATE_ATTACK);
				player.setAnimation(Entity.ANI_ATTACK);
			}
		}
		return false;
	}

	public boolean touchDragged(TouchEvent event) {
		if(event.pointer != touchPointer) return false;
		
		if(touched == true) {
			if(padArea.contains(event.x, event.y)) {
			}
		}
		return false;
	}

	public boolean touchUp(TouchEvent event) {
		if(event.pointer == touchPointer){
			touched = false;
			touchPointer = -1;
		}
		return false;
	}
	
	public void present(Graphics g) {
		if(touched == true) {
			g.drawBitmap(onImage, padArea.left, padArea.top);
		} else {
			g.drawBitmap(Image, padArea.left, padArea.top);
		}
	}
}
